package com.lingyistudio.common.util;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author :  LingYi
 * @ClassName: ThreadModel
 * @Date :   2019/6/26
 */

@Slf4j
public class ThreadModel extends Thread {
    static ThreadModel threadModel  = new ThreadModel();
    int num = 0;
    private ThreadModel(){};
    public static ThreadModel getThreadModel(){
        return threadModel;
    }
    @Override
    public void run() {
        this.setName("longTerm");
        go();
    }
    private void go() {
        TimeModel timeModel = TimeModel.getTimeModel();
        AESModel aesModel = AESModel.getAESModel();
        int aesModelMinute=-1;
        while (true){
            setNum();
            try {
                Thread.sleep(1); //休眠1毫秒
            } catch (InterruptedException e) {
                log.error("common.util.ThreadModel.go  "+String.valueOf(e));
            }
            if(this.num%250==0){
                timeModel.setTime();    //约每250毫秒更新一次时间
                if (this.num%1000==0){ //每秒进入一次判断
                    if(aesModelMinute!=timeModel.minute){//每分钟更新一次aes密码
//                        log.info("线程获取");
                        aesModel.setKEY();
                        aesModelMinute=timeModel.minute;
                    }
                }
            }
        }
    }

    public void setNum() {
        this.num = this.num++;
        if (num==60000){
            this.num=0;
        }
    }
}
